<?php
function delete_kesehatan_biaya($id){
	$query = "DELETE FROM kes_biaya_kesehatan WHERE biaya_kesehatan_id = {$id} ";
	return my_query($query);
}

function list_biaya_kesehatan(){
 
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
			}
			return false;
		}
	');		 

	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Tanggal'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'Periode'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Nama Karyawan'=>array('style'=>'border-bottom:2px solid;width:30%'),  
		'Hub. keluarga'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),   
		'Unit'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),   
		'Biaya'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'),   
		' '=>array('style'=>'border-bottom:2px solid;width:5%'), 
	);
	if(isset($_GET['av'])){
		if(isset($_GET['key']))
			$query = "SELECT * FROM kes_biaya_kesehatan a
				INNER JOIN kes_unit b ON a.unit_id = b.unit_id 
				INNER JOIN karyawan c ON a.karyawan_id = c.karyawan_id
				INNER JOIN biodata d ON d.biodata_id = c.biodata_id
				WHERE c.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable) AND 
				( c.nik='{$_GET['key']}' OR d.nama LIKE '%{$_GET['key']}%' OR b.nama_unit ='{$_GET['key']}')
				ORDER BY  a.tanggal DESC, biaya_kesehatan_id DESC";
		else
			$query = "SELECT * FROM kes_biaya_kesehatan a
				INNER JOIN kes_unit b ON a.unit_id = b.unit_id
				ORDER BY tanggal DESC, biaya_kesehatan_id ASC";
	}else{
		$periode_id = get_periode_absen();
		$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
		if(isset($_GET['key']))
		$query = "SELECT * FROM kes_biaya_kesehatan a
			INNER JOIN kes_unit b ON a.unit_id = b.unit_id 
			INNER JOIN karyawan c ON a.karyawan_id = c.karyawan_id
			INNER JOIN biodata d ON d.biodata_id = c.biodata_id
			WHERE c.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable) AND (  a.tanggal BETWEEN '{$periode['dari']}' AND '{$periode['sampai']}' ) AND 
			( c.nik='{$_GET['key']}' OR d.nama LIKE '%{$_GET['key']}%' OR b.nama_unit ='{$_GET['key']}')
			ORDER BY  a.tanggal DESC, biaya_kesehatan_id DESC";
		else
			$query = "SELECT * FROM kes_biaya_kesehatan a
			INNER JOIN kes_unit b ON a.unit_id = b.unit_id
			WHERE (  a.tanggal BETWEEN '{$periode['dari']}' AND '{$periode['sampai']}' )  
			ORDER BY tanggal DESC, biaya_kesehatan_id ASC";
	}
	//if(isset($_GET['ho']))print($query);
	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =35;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&av={$_GET['av']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 35; 
	while( $ey = my_fetch_array($result) ){
		$i++;
		$t = my_get_data_by_id('kacamata','biaya_kesehatan_id',$ey['biaya_kesehatan_id']);
		if( $t ){
			$delete_button = button_icon( 'b_drop_off.png' , ''  );
		}else{	
			$t = my_get_data_by_id('penggantian_karyawan','biaya_kesehatan_id',$ey['biaya_kesehatan_id']);
			if( $t ){
				$delete_button = button_icon( 'b_drop_off.png' , ''  );
			}else{
				$deleteproperty = array(
					'href'=>'javascript:; ',
					'onclick'=>'javascript:confirmDelete('.$ey['biaya_kesehatan_id'].');',
					'title'=>'Delete'
				);
				$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
			}
		}	
		
		$viewproperty = array(
			'href'=>'index.php?com=kes_biaya&task=view_kes_biaya&id='.$ey['biaya_kesehatan_id'],
			'rel'=>'facebox',
			'title'=>'View'
		);
		$view_button = button_icon( 'b_search.png' , $viewproperty  );			
		$periodes=my_get_data_by_id('pr_periode','periode_id', get_periode_id_by_tanggal( $ey['tanggal'] ));
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		$hubungankeluargas = my_get_data_by_id('hubungan_keluarga','hubungan_keluarga_id', ( isset($ey['keluarga_id']) ? $ey['keluarga_id'] : 0) );
		
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'tgl'=> position_text_align ( date( 'd-m-Y' , strtotime($ey['tanggal']) ) ,  'center'), 
			'periode'=> position_text_align ( date( 'm-Y' , strtotime($periodes['tanggal']) ) ,  'center'), 
			'nik'=>  $karyawan['nik'] ,
			'nama'=>  $karyawan['nama_gelar'] ,
			'hubungan'=>  position_text_align($hubungankeluargas['label'] ,'center'),
			'unit'=>  position_text_align ($ey['nama_unit'] ,  'center'), 
			'biaya'=> position_text_align( rp_format( $ey['total_biaya']) ,  'right'),  
			'ops'=> position_text_align( $view_button.' '.$delete_button , 'right'),  
		);
	}
	$datas = table_rows($row); 
	if(isset($_GET['av'])){
		$menuontop = array(  
			'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com='.$_GET['com'].'&task=plaincetak&key='.$_GET['key'].'&av='.$_GET['av'].'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,scrollbars,resizable=0\');'),
			'Upload data'=>array('onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com='.$_GET['com'].'&task=upload\' });'),
			'Tambah data'=>array('onclick'=>'javascript:location.href=\'index.php?com=kes_biaya&task=edit\''),
			'This Periode '=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'\'')
		);
	}else{
		$menuontop = array(  
			'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com='.$_GET['com'].'&task=plaincetak&key='.$_GET['key'].'&av='.$_GET['av'].'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,scrollbars,resizable=0\');'),
			'Upload data'=>array('onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com='.$_GET['com'].'&task=upload\' });'),
			'Tambah data'=>array('onclick'=>'javascript:location.href=\'index.php?com=kes_biaya&task=edit\''),
			'All Periode'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&av=true\' ')
	);
	}
	$search = '<form method="GET">
	<input type="hidden" name="com" value="'.$_GET['com'].'" />'.
		( isset($_GET['av']) ? 	
			'<input type="hidden" name="av" value="'.$_GET['av'].'" />'	:	''
	).' <span style="font:10px verdana"><b>Pencarian cepat</b></span>
	<br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" />
	<input class="button" type="submit" value=" Cari " />  </form>';
	$printmenu = additional_menu_on_list( $menuontop , $search);
	$paging = $kgPagerOBJ ->showPaging();
	return $printmenu .table_builder($header , $datas , 9 , false ,$paging); 
}


function upload_temp(){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
			}
			return false;
		}
	');		 

	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Tanggal'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:10%'),  
	 	'Unit'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),   
		'Biaya'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'),   
	//' '=>array('style'=>'border-bottom:2px solid;width:5%'), 
	);
	$query = "SELECT * FROM kes_biaya_kesehatan_temp a
	INNER JOIN kes_unit b ON a.unit_id = b.unit_id
	ORDER BY tanggal DESC, biaya_kesehatan_id ASC";
	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =35;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
			$kgPagerOBJ->pager_set(
				$pager_url, 
				$total_records, 
				$scroll_page, 
				$per_page, 
				$current_page, 
				$inactive_page_tag, 
				$previous_page_text, 
				$next_page_text, 
				$first_page_text, 
				$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 35; 
	while( $ey = my_fetch_array($result) ){
		$i++;
		$editproperty = array(
			'href'=>'index.php?com=kes_biaya&task=edit&id='.$ey['biaya_kesehatan_id'],
			'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
			'href'=>'javascript:; ',
			'onclick'=>'javascript:confirmDelete('.$ey['biaya_kesehatan_id'].');',
			'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'tgl'=> position_text_align ( date( 'd-m-Y' , strtotime($ey['tanggal']) ) ,  'center'), 
			'nik'=>  $karyawan['nik'] ,
		 	'unit'=>  position_text_align ($ey['nama_unit'] ,  'center'), 
			'biaya'=> position_text_align( rupiah_format( $ey['total_biaya']) ,  'right'),  
			//'ops'=> position_text_align(   $delete_button , 'right'),  
		);
	}
	$datas = table_rows($row); 
	$menuontop = array(  
	//	'Upload data'=>array('onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com='.$_GET['com'].'&task=upload\' });'),
		'Empty'=>array('onclick'=>'javascript:location.href=\'index.php?com=kes_biaya&task=viewupload_cancel\''),
		'Simpan'=>array('onclick'=>'javascript:location.href=\'index.php?com=kes_biaya&task=viewupload_save\''),
	);
	$printmenu = additional_menu_on_list( $menuontop , '');
	$paging = $kgPagerOBJ ->showPaging();
	return $printmenu .table_builder($header , $datas , 9 , false ,$paging); 
}


function form_kes_biaya_create( $id =0 ){
	my_set_file_js(
		array(
			'components/system/jquery/combomulti/jquery.chainedSelects.js',
			'components/system/js/calendar/calendarDateInput.js' 
		)
	);
	
	my_set_file_js(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.js'  
		)
	);
	my_set_file_css(
		array(
				'components/system/jquery/autocomplete/jquery.autocomplete.css' 
			)
	);

	my_set_code_js('  
		function findValue(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

		function selectItem(li) {
			findValue(li);
		}

		function formatItem(row) {
			return   row[0] ;
		}

		function lookupAjax(){
			var oSuggest = $("#nama_karyawan")[0].autocompleter;
			oSuggest.findValue(); 
			return false;
		}
		$(document).ready(function() {
			$("#nama_karyawan").autocomplete(
				"autocomplete_daftar_karyawan.php",
				{
					delay:10,
					minChars:2,
					matchSubset:1,
					matchContains:1,
					cacheLength:5,
					onItemSelect:selectItem,
					onFindValue:findValue,
					formatItem:formatItem,
					autoFill:true
				}
			);
			 
		});

		function checkName(name){
			$.get("check_karyawan_names.php", { nama: name  },
			   function(data){
				 if(data ==  \'0\' ){
					alert(\'Nama karyawan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\');
					$(\'#nama_karyawan\').val(\'\');
					return false;
				 } 
			   });
		}
	');	

	$view = form_header( "seleski" , "seleski"  );
 	$fields = my_get_data_by_id('kes_biaya_kesehatan','biaya_kesehatan_id',$id);
	
	$ftanggal = $ftanggalm = $ftanggals = date('d-m-Y');
	if($fields){
		list($yyyytanggal , $mmtanggal,$ddtanggal ) = explode("-" ,$fields['tanggal'] );
		$ftanggal = $ddtanggal.'-'.$mmtanggal.'-'.$yyyytanggal;
	}	 
	$tanggal = array(
		'name'=>'tanggal',
		'value'=>(isset($_POST['tanggal'])? $_POST['tanggal'] : $ftanggal),
		'id'=>'tanggal',
		'type'=>'textfield',
		'size'=>'45'
	);
	$form_tanggal = form_calendar($tanggal);
	$view .= form_field_display( $form_tanggal  , "Tanggal transaksi" );

	$karyawan_data = loaddata_karyawan(( $fields ? $fields['karyawan_id'] : 0));
	$karyawan = array(
		'name'=>'nama_karyawan',
		'value'=>(isset($_POST['nama_karyawan'])? $_POST['nama_karyawan'] : ($fields ? $karyawan_data['nik'].'/'.$karyawan_data['nama'] :'')  ),
		'id'=>'nama_karyawan',
		'type'=>'textfield',
		'size'=>'35'
	);
	$form_karyawan = form_dynamic($karyawan);
	$view .= form_field_display( $form_karyawan  , "NIK/Nama Karyawan"  );
	
	$unit_check = "SELECT * FROM kes_unit";
	$result = my_query($unit_check);
	$units = array();
	while( $re = my_fetch_array($result) ){
		$units[$re['unit_id']] = $re['nama_unit'];
	}
	 
	$unit = array(
		'name'=>'unit_id',
		'value'=>(isset($_POST['unit_id'])? $_POST['unit_id'] : $fields['unit_id'] ),
		'id'=>'unit_id' 
	);
	$unit_form = form_dropdown( $unit , $units );
	$view .= form_field_display(  $unit_form , "Unit pengobatan"  );
		 
	$nama_pasien = array(
		'name'=>'nama_pasien',
		'value'=>(isset($_POST['nama_pasien'])? $_POST['nama_pasien'] : $fields['nama_pasien'] ),
		'id'=>'nama_pasien','size'=>'55',
		'type'=>'textfield' 
	);
	$nama_pasien_form = form_dynamic($nama_pasien);
	$view .= form_field_display(  $nama_pasien_form , "Nama Pasien"  );
	 
	$no_pasien = array(
		'name'=>'no_pasien',
		'value'=>(isset($_POST['no_pasien'])? $_POST['no_pasien'] : $fields['no_pasien'] ),
		'id'=>'no_pasien','size'=>'25',
		'type'=>'textfield' 
	);
	$no_pasien_form = form_dynamic($no_pasien);
	$view .= form_field_display(  $no_pasien_form , "Nomor Pasien"  );
	
	$hubungan_keluarga = array(
		'name'=>'hubungan_keluarga',
		'value'=>(isset($_POST['hubungan_keluarga'])? $_POST['hubungan_keluarga'] : $fields['hubungan_keluarga'] ),
		'id'=>'hubungan_keluarga','size'=>'1',
		'type'=>'textfield' 
	);
	$hubungan_keluarga_form = form_dynamic($hubungan_keluarga);
	$view .= form_field_display(  $hubungan_keluarga_form , "Hubungan Keluarga"  );
	
			 
	$noreg = array(
		'name'=>'noreg',
		'value'=>(isset($_POST['noreg'])? $_POST['noreg'] : $fields['noreg'] ),
		'id'=>'noreg','size'=>'25',
		'type'=>'textfield' 
	);
	$noreg_form = form_dynamic($noreg);
	$view .= form_field_display(  $noreg_form , "Nomor Registrasi"  );	
	
	$nama_dokter = array(
		'name'=>'nama_dokter',
		'value'=>(isset($_POST['nama_dokter'])? $_POST['nama_dokter'] : $fields['nama_dokter'] ),
		'id'=>'nama_dokter','size'=>'55',
		'type'=>'textfield' 
	);
	$nama_dokter_form = form_dynamic($nama_dokter);
	$view .= form_field_display(  $nama_dokter_form , "Nama Dokter"  );
	
	
	$kode_dokter = array(
		'name'=>'kode_dokter',
		'value'=>(isset($_POST['kode_dokter'])? $_POST['kode_dokter'] : $fields['kode_dokter'] ),
		'id'=>'kode_dokter',
		'size'=>'25',
		'type'=>'textfield' 
	);
	$kode_dokter_form = form_dynamic($kode_dokter);
	$view .= form_field_display(  $kode_dokter_form , "Kode Dokter"  );
	$pos_check = "SELECT * FROM pos_biaya";
	$result_pos = my_query($pos_check);
	$costs = array();
	while( $res = my_fetch_array($result_pos) ){
		$costs[$res['pos_biaya_id']] = $res['code'] .' | '.$res['label'] ;
	}
	 
	$cost = array(
		'name'=>'pos_biaya_id',
		'value'=>(isset($_POST['pos_biaya_id'])? $_POST['pos_biaya_id'] : $fields['pos_biaya_id'] ),
		'id'=>'pos_biaya_id' 
	);
	$unit_cost = form_dropdown( $cost , $costs );
	$view .= form_field_display(  $unit_cost , "Cost Center"  );
	
	$total_biaya = array(
		'name'=>'total_biaya',
		'value'=>(isset($_POST['total_biaya'])? $_POST['total_biaya'] : rupiah_format($fields['total_biaya']) ),
		'id'=>'total_biaya',
		'size'=>'15',
		'type'=>'textfield' 
	);
	$total_biaya_form = form_money($total_biaya);
	$view .= form_field_display(  'Rp. '.$total_biaya_form , "Total Biaya"  );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 

	$back = array(
		'value' => ' Batal  ' ,
		'name' => 'back', 'onclick'=>'location.href=\'index.php?com=kes_biaya\'',
		'type'=>'button','class'=>'main_button'
	);
	$form_back= form_dynamic($back); 
	$view .= form_field_display( $form_submit .' '. $form_back  , "&nbsp;" ,  "" );	
	$view .= form_footer( );
	return $view;
}


function form_kes_biaya_submit($id = 0){
	$karyawan_id = check_karyawan_by_nik_nama($_POST['nama_karyawan']);

	if($_POST){
		list( $ddtanggal, $mmtanggal,$yyyytanggal ) = explode("-" ,$_POST['tanggal'] );
		$ftanggal = $yyyytanggal.'-'.$mmtanggal.'-'.$ddtanggal;
	}
	$datas = array(
	'tanggal'		=>my_type_data_str($ftanggal), 
	'karyawan_id'	=>my_type_data_int($karyawan_id),
	'unit_id' 		=>my_type_data_str($_POST['unit_id']),	 
	'pos_biaya_id' 	=> my_type_data_str($_POST['pos_biaya_id']),	 
	'no_pasien' 	=> my_type_data_str($_POST['no_pasien']),	 
	'keluarga_id'=> my_type_data_int($_POST['hubungan_keluarga']),	 
	'noreg' 		=> my_type_data_str($_POST['noreg']),	 
	'total_biaya' => my_type_data_str( set_text_as_rupiah($_POST['total_biaya'])),	 
	'nama_pasien' => my_type_data_str($_POST['nama_pasien']),	 
	'nama_dokter' => my_type_data_str($_POST['nama_dokter']),	 
	'kode_dokter' => my_type_data_str($_POST['kode_dokter']),
	'tanggal_upload'=>	 my_type_data_function('NOW()'),
	);
	 
	if($id > 0){	
		return my_update_record('kes_biaya_kesehatan','biaya_kesehatan_id',$id,$datas);
	}
	$new_id = my_insert_record('kes_biaya_kesehatan',$datas);
	return $new_id;
}



function  form_kes_biaya_upload( ){ 
  
	$view = form_header( "seleski" , "seleski"  );
   
	$tanggal = array(
		'name'=>'fname', 
		'id'=>'fname',
		'type'=>'file',
		'size'=>'45'
	);
	$form_tanggal = form_dynamic($tanggal);
	$view .= form_field_display( $form_tanggal  , "File csv" );

	$submit = array(
		'value' =>  ' Upload ' ,
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 

	$back = array(
		'value' => ' Batal  ' ,
		'name' => 'back', 'onclick'=>'location.href=\'index.php?com=kes_biaya\'',
		'type'=>'button','class'=>'main_button'
	);
	$form_back= form_dynamic($back); 
	$view .= form_field_display( $form_submit   , "&nbsp;" ,  "" );	
	$view .= form_footer( );
	return $view;
}

function  form_kes_biaya_upload_submit( ){
	$path  = "files/upload/obat/";
	$file_upload = $path.date('Ymd_His').'.csv'; 
	if($t = move_uploaded_file($_FILES['fname']['tmp_name'] , $file_upload)){
		
		get_data_and_stor_obat($file_upload);
		return true;
	} 
	return false;
}

function get_data_and_stor_obat($file_upload){ 
	$handle = fopen($file_upload, "r");
	while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) { 
		if($data[4]=='NOPASIEN')continue;
		stor_each_item_data($data); 
	}
	fclose($handle); 
}

function stor_each_item_data($data){
	$karyawan_id = check_karyawan_by_nik($data[1]);
	$ftanggal = date('Y-m-d', (  $data[6] - 25569  ) * 24* 3600   );
	$periode_id = get_periode_id_by_tanggal_obat($tanggal);
	$datas = array(
		'tanggal'		=>my_type_data_str($ftanggal), 
		'karyawan_id'	=>my_type_data_int($karyawan_id),
		'unit_id' 		=>my_type_data_str(get_unit_id($data[0])),	 
		'pos_biaya_id' 	=> my_type_data_str(get_pos_biaya_id($data[7])),	 
		'no_pasien' 	=> my_type_data_str($data[4]),	 
		'keluarga_id'=> my_type_data_int($data[2]),	 
		'noreg' 		=> my_type_data_str($data[5]),	 
		'total_biaya' => my_type_data_str( $data[10] ),	 
		'nama_pasien' => my_type_data_str($data[3]),	 
		'nama_dokter' => my_type_data_str($data[9]),	 
		'kode_dokter' => my_type_data_str($data[8]),	 
		'periode_id' => my_type_data_str($periode_id),	 
	);
	  
	$new_id = my_insert_record('kes_biaya_kesehatan_temp',$datas);
}

function get_periode_id_by_tanggal_obat($tanggal){
	$tanggal = date( 'Y-m-01' , strtotime($tanggal));
	$query = "SELECT periode_id FROM pr_periode WHERE tanggal = '{$tanggal}' LIMIT 1";
	$res = my_query($query);
	$row = my_fetch_array($res);
	return $row['periode_id'];
}

function get_unit_id($str){
	$query = "SELECT unit_id FROM kes_unit WHERE nama_unit ='{$str}'	";
	$res = my_query($query);
	$row = my_fetch_array($res);
	return (int) $row['unit_id'];
}

function get_pos_biaya_id($code){
	$query = "SELECT pos_biaya_id FROM pos_biaya WHERE code ='{$str}'	";
	$res = my_query($query);
	$row = my_fetch_array($res);
	return (int) $row['pos_biaya_id'];
}


function view_kes_biaya( $id ){
 	$fields = my_get_data_by_id('kes_biaya_kesehatan','biaya_kesehatan_id',$id);
	$periodes=my_get_data_by_id('pr_periode','periode_id', get_periode_id_by_tanggal( $fields['tanggal'] ));
	$karyawan = loaddata_karyawan($fields['karyawan_id']);
	$unitpengobatans = my_get_data_by_id('kes_unit','unit_id', ( isset($fields['unit_id']) ? $fields['unit_id'] : 0) );
	$posbiayas = my_get_data_by_id('pos_biaya','pos_biaya_id', ( isset($fields['pos_biaya_id']) ? $fields['pos_biaya_id'] : 0) );
	$hubungankeluargas = my_get_data_by_id('hubungan_keluarga','hubungan_keluarga_id', ( isset($fields['keluarga_id']) ? $fields['keluarga_id'] : 0) );

	$view = form_header( "seleski" , "seleski"  );
	$view .= form_field_display( date( 'd-m-Y' , strtotime($fields['tanggal']) ) ,"Tanggal"    );
	$view .= form_field_display( $karyawan['nik'].'/'.$karyawan['nama_gelar'] ,"NIK / Nama Karyawan"    );
	$view .= form_field_display( $unitpengobatans['nama_unit'] ,"Unit Pengobatan"    );
	$view .= form_field_display( $fields['nama_pasien'] ,"Nama Pasien"    );
	$view .= form_field_display( $fields['no_pasien'] ,"Nomor Pasien"    );
	$view .= form_field_display( $fields['noreg'] ,"Nomor Register"    );
	$view .= form_field_display( $hubungankeluargas['label'] ,"Hubungan Keluarga"    );
	$view .= form_field_display( $fields['nama_dokter'] ,"Nama Dokter"    );
	$view .= form_field_display( $fields['kode_dokter'] ,"Kode Dokter"    );
	$view .= form_field_display( $posbiayas['code'].' / '.$posbiayas['label'] ,"Cost Center"    );
	$view .= form_field_display( 'Rp. '.rp_format($fields['total_biaya']) ,"Total Biaya"    );
	$view .= form_footer( );
	return $view;
}

function print_plain($key,$av){
	$query = "SELECT * FROM kes_biaya_kesehatan a
		INNER JOIN kes_unit b ON a.unit_id = b.unit_id 
		INNER JOIN karyawan c ON a.karyawan_id = c.karyawan_id
		INNER JOIN biodata d ON d.biodata_id = c.biodata_id
		WHERE 1 ";
	if( trim( $key )=="" ){
	}else{
		$query .= " AND ( c.nik='{$key}' OR d.nama LIKE '%{$key}%' OR b.nama_unit ='{$key}')" ;
	}	
	if( trim( $av )=="" ){
		$periode_id = get_periode_absen();
		$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
		$query .= " AND (  a.tanggal BETWEEN '{$periode['dari']}' AND '{$periode['sampai']}' ) " ;
	}			
	$query .= " ORDER BY  a.tanggal DESC, biaya_kesehatan_id DESC";

	$result = my_query($query ); 
	$datas = array();  
	$datas[] = array(
		 'k1'=>array("No." ,5 ,STR_PAD_BOTH),
		 'k2'=>array("Tanggal" ,10,STR_PAD_RIGHT),
		 'k3'=>array("Periode" ,10,STR_PAD_RIGHT),
		 'k4'=>array("NIK" ,10,STR_PAD_RIGHT),
		 'k5'=>array("Nama Karyawan" ,30,STR_PAD_RIGHT),  
		 'k6'=>array("Hub. Keluarga" ,15,STR_PAD_RIGHT),  
		 'k7'=>array("Unit" ,10,STR_PAD_RIGHT),  
		 'k8'=>array("Biaya" ,10,STR_PAD_LEFT),  
	); 
	$datas[] = array(	//FOR HEADER SPACE (NECK)
		 'k1'=>array(" " ,100,STR_PAD_BOTH),
	); 
	$i=0;
	while($ey = my_fetch_array($result)){
		$periodes=my_get_data_by_id('pr_periode','periode_id', get_periode_id_by_tanggal( $ey['tanggal'] ));
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		$hubungankeluargas = my_get_data_by_id('hubungan_keluarga','hubungan_keluarga_id', ( isset($ey['keluarga_id']) ? $ey['keluarga_id'] : 0) );

		$i++; 		
		$datas[] = array(		 
			'k1'=>array($i ,5 ,STR_PAD_BOTH),
			'k2'=>array( date( 'd-m-Y' , strtotime($ey['tanggal']) ),10,STR_PAD_RIGHT),
			'k3'=>array( date( 'm-Y' , strtotime($periodes['tanggal']) ) ,10,STR_PAD_RIGHT),
			'k4'=>array( $karyawan['nik'] ,10,STR_PAD_RIGHT),
			'k5'=>array( $karyawan['nama_gelar'] ,30,STR_PAD_RIGHT),  
			'k6'=>array( $hubungankeluargas['label'] ,15,STR_PAD_RIGHT),  
			'k7'=>array( $ey['nama_unit'] ,10,STR_PAD_RIGHT),  
			'k8'=>array( rp_format($ey['total_biaya']),10,STR_PAD_LEFT),  
		);
	}
	$view = 	str_pad("CETAK BIAYA KESEHATAN",100," ",STR_PAD_BOTH);
	return $view ."\n"."\n".set_txt_report($datas);
}

